package model;

import java.util.HashMap;
import java.util.Map;

public class Borrower {
    private final String id;
    private final String name;
    private final Map<String, Resource> borrowedResources;

    public Borrower(String id, String name) {
        this.id = id;
        this.name = name;
        this.borrowedResources = new HashMap<>();
    }


    public boolean borrowResource(Resource resource) {
        if (borrowedResources.containsKey(resource.getId())) {
            return false; // 已借阅该资源
        }

        if (resource.borrow(this.id)) {
            borrowedResources.put(resource.getId(), resource);
            return true;
        }
        return false;
    }


    public boolean returnResource(String resourceId) {// 借阅资源
        Resource resource = borrowedResources.get(resourceId);
        if (resource == null) return false;

        resource.returnResource();
        borrowedResources.remove(resourceId);
        return true;
    }

    // Getters// 归还资源
    public String getId() { return id; }
    public String getName() { return name; }
    public Map<String, Resource> getBorrowedResources() {
        return new HashMap<>(borrowedResources);
    }
}
